//
//  XXListView.m
//  XX
//
//  Created by lly on 15/5/26.
//  Copyright (c) 2015年 lly. All rights reserved.
//

#import "XXListView.h"
#import "UIViewExt.h"
#import "AppMacro.h"
#define SEC_WIDTH_DEFAULT 120.0
@implementation XXListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.sectionTable = [[UITableView alloc] init];
        self.sectionTable.dataSource = self;
        self.sectionTable.delegate   = self;
        self.sectionTable.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
        self.sectionTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.sectionTable];
        
        self.listTable = [[UITableView alloc] init];
        self.listTable.dataSource = self;
        self.listTable.delegate  = self;
//        self.listTable.backgroundColor = [UIColor blueColor];
        [self addSubview:self.listTable];
        
        //data initlize
        self.sectionTableWidth = SEC_WIDTH_DEFAULT; //default
        self.listTableWidth    =  SCREEN_WIDTH - SEC_WIDTH_DEFAULT;  //default
        self.currentSection    = 0;
        
        self.sectionTable.frame = CGRectMake(0, 0, self.sectionTableWidth, self.height);
        self.listTable.frame = CGRectMake(self.sectionTableWidth, 0, self.listTableWidth, self.height);
        NSLog(@"%@",NSStringFromCGRect(self.sectionTable.frame));
        NSLog(@"%@",NSStringFromCGRect(self.listTable.frame));

        
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"%@",NSStringFromCGRect(self.sectionTable.frame));
    NSLog(@"%@",NSStringFromCGRect(self.listTable.frame));
    
}



#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.sectionTable)
    {
        return [self.delegate numberOfSection:self];
    }
    else if(tableView == self.listTable)
    {
        return [self.delegate listView:self numberOfRowInSection:self.currentSection];
        
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.sectionTable)
    {
        return [self.delegate sectionListView:self cellForSection:indexPath.row];
    }
    else if(tableView == self.listTable)
    {
        return [self.delegate listListView:self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:self.currentSection]];
    }
    
    return nil;
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.sectionTable)
    {
        self.currentSection = indexPath.row;
        [self.listTable reloadData];
    }
    else if(tableView == self.listTable)
    {
        [self.delegate listView:self didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:self.currentSection]];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.sectionTable)
    {
        return [self.delegate heightForSectionAtIndex:indexPath.row];
    }
    else if(tableView == self.listTable)
    {
        return  [self.delegate listView:self heightForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:self.currentSection]];
    }
    return 0;
}



@end
